import { MessageBox } from 'mint-ui'
import { getImageUrl } from '@/assets/js/ApiUtil'
import conf from '@/assets/js/conf'

export function uploadUntil (el, callback) {
  let file = el.files[0]
  if (file) {
    const isJPG = file.type === 'image/jpeg'
    const isLt2M = file.size / 1024 / 1024 < 2
    if (!isJPG) {
      MessageBox('上传头像图片只能是 JPG 格式!')
      return false
    }
    if (!isLt2M) {
      MessageBox('上传头像图片大小不能超过 2MB!')
      return false
    }
    if (el.files && el.files[0]) {
    // let imgSrc = window.navigator.userAgent.indexOf('Chrome') >= 1 || window.navigator.userAgent.indexOf('Safari') >= 1 ? window.webkitURL.createObjectURL(filHead.files[0]) : window.URL.createObjectURL(filHead.files[0])
    // console.log(imgSrc)
      let formData = new FormData()
      formData.append('files', file)
      getImageUrl(formData).then(resp => {
        let data = resp.data
        if (data && data instanceof Array) {
          callback(data)
        }
      })
    } else {
      el.select()
      el.blur()
    }
  } else {
    console.log('取消上传')
  }
  
}

export function buildImage (path) {
  if (path && path.length > 0) {
      return conf.baseUrl + "/image/" + path
  }
  return null
}